from lambwaves import Lamb
import tqdm
from typing import List, Tuple

from pylab import *
from matplotlib import rcParams
# 最佳中文配置
rcParams.update({
    'font.family': ['sans-serif'],
    'font.sans-serif': [
        'Noto Sans CJK JP',  # 中文字体（思源黑体）
        'Nimbus Sans',       # 英文字体
        'DejaVu Sans',       # 备用英文字体
        'Arial',             # 备用
        'sans-serif'         # 最终备用
    ],
    'font.size': 10,
    'axes.titlesize': 12,
    'axes.labelsize': 11,
    'xtick.labelsize': 9,
    'ytick.labelsize': 9,
    'legend.fontsize': 9,
    
    # 数学字体设置（重要！）
    'mathtext.fontset': 'stix',      # 使用STIX数学字体
    'mathtext.default': 'regular',   # 常规样式
})
DPI=300
 

# FMAX = 25 # unit: Hz
# FMIN = 14 # unit: Hz
# D = 8 # unit: m
# CS = 1400 # unit: m/s
# freqs = np.linspace(FMIN, FMAX, 100)

# CMAX = 10000

# D_choices = np.arange(3,15,0.5)
# CS_choices = np.arange(1000,2500,100)
# ratio = 2
# ND, NC = len(D_choices), len(CS_choices)
# results = np.zeros((ND,NC))
# for i,D in enumerate(D_choices):
#     for j,CS in enumerate(CS_choices):
#         CP = 4000 # unit: m/s

#         alum = Lamb(thickness=D*1000,  #unit: mm 
#                     nmodes_sym=0,     # mode
#                     nmodes_antisym=1, # mode
#                     fd_max=FMAX*D,     # fd/thickness 是 kHz*mm
#                     vp_max=CMAX,     # unit: m/s
#                     c_L=CP,         # unit: m/s
#                     c_S=CS,
#                     fd_points=25
#                     )         # unit: m/s

#         cpi = alum.vp_antisym['A0'](freqs*D)
#         rms = np.mean((cpi-750)**2)
#         results[i,j] = rms
#         print(f'D={D:.1f}, rms={rms:.1f}')
# X,Y = np.meshgrid(D_choices, CS_choices)
# results = results.T
# fig, ax = plt.subplots(figsize=(6,4),dpi=DPI)
# ax.pcolormesh(X,Y,results)
# print(results.min())
# print(results.max())
# # 标记最大最小值与位置
# ax.scatter(X[np.where(results==results.min())],Y[np.where(results==results.min())],s=50,c='r',marker='*',label='min')
# ax.text(X[np.where(results==results.min())],Y[np.where(results==results.min())],results.min(),fontsize=8)
# ax.set_xlabel('D (m)')
# ax.set_ylabel('CS (m/s)')
# ax.set_title(f'RMS, ratio = {ratio:.1f}')

# fig.savefig('alum.rms.png',dpi=DPI)

FMAX = 200 # unit: Hz
FMIN = 1 # unit: Hz
D = 10 # unit: m
CS = 1400 # unit: m/s
CP = 3500 # unit: m/s
freqs = np.linspace(FMIN, FMAX, 100)

CMAX = 10000

alum = Lamb(thickness=D*1000,  #unit: mm 
                    nmodes_sym=4,     # mode
                    nmodes_antisym=4, # mode
                    fd_max=FMAX*D,     # fd/thickness 是 kHz*mm
                    vp_max=CMAX,     # unit: m/s
                    c_L=CP,         # unit: m/s
                    c_S=CS,
                    fd_points=100
                    )         # unit: m/s
fig, ax = alum.plot_phase_velocity()
current_ticks = ax.get_xticks()
new_ticks = current_ticks  # 使用相同的刻度位置
new_labels = [f'{tick/D:.1f}' for tick in new_ticks]  # 转换为频率值
ax.set_xticks(new_ticks)
ax.set_xticklabels(new_labels)
# ax.set_ylim(0,1200)
ax.plot([FMIN*D,FMAX*D], [750, 750],'--', label='cs', lw=1,color = 'k')
ax.set_xlim(FMIN*D,FMAX*D)
ax.set_xlabel('Frequency (Hz)')  # 更新横轴标签

fig.savefig('alum.phase.velocity.png',dpi=DPI)


# fig = figure(figsize=(6,4),dpi=DPI)
# ax = fig.add_subplot(111)

# for mode, arr in alum.vp_sym.items():
#     fd = np.arange(np.amin(arr.x), np.amax(arr.x), 0.1)
#     c = alum.vp_sym[mode](fd)
#     ax.plot(fd/D, c, label=f'sym {mode}', lw=1,color = 'r')   

# for mode, arr in alum.vp_antisym.items():
#     fd = np.arange(np.amin(arr.x), np.amax(arr.x), 0.1)
#     c = alum.vp_antisym[mode](fd)
#     ax.plot(fd/D, c, label=f'asym {mode}', lw=1,color = 'r')   

# ax.plot([FMIN, FMAX], [CS, CS],'--', label='cs', lw=1,color = 'k')
# ax.plot([FMIN, FMAX], [CP, CP],'--', label='cp', lw=1,color = 'k')
# xlabel('frequency (Hz)')
# ylabel('phase velocity (m/s)')
# legend()
# ax.set_xlim(FMIN,FMAX)


# fig.savefig('my.png')


